var testdata={
	totalNum: 1323,
	totalMoney: 6566544,
	charts: [
		{
			xdata: '2017-2-1',
			ydata1: 223,
			ydata2: 2344
		},{
			xdata: '2017-2-2',
			ydata1: 122,
			ydata2: 3233
		},{
			xdata: '2017-2-3',
			ydata1: 231,
			ydata2: 4434
		},{
			xdata: '2017-2-4',
			ydata1: 22,
			ydata2: 2232
		},{
			xdata: '2017-2-5',
			ydata1: 212,
			ydata2: 1232
		},{
			xdata: '2017-2-6',
			ydata1: 234,
			ydata2: 3343
		}
	]
};




$(function(){
	getShowMes(1);
    $("#statisticsTitle").text("按品牌类型盘点");
	$("#statisticsST").unbind().change(function(){
		var statisticsST = $("#statisticsST").val();
		if(statisticsST==1){
			$("#statisticsTitle").text("按品牌类型盘点");
		}else if(statisticsST==2){
			$("#statisticsTitle").text("按产品类型盘点");
		}else if(statisticsST==3){
            $("#statisticsTitle").text("按年盘点");
        }else if(statisticsST==4){
            $("#statisticsTitle").text("按月盘点");
        }else if(statisticsST==5){
            $("#statisticsTitle").text("按日盘点");
        }
		getShowMes(statisticsST);
	});
    $("#btnExcel").on('click',function () {
        excelsearchData();
    });
	
});
/*查询excel数据，并导出*/
function excelsearchData(){
    $.post(
        gobalServer+"order/downloadOrderUserMessage",
		{},
        function(backData){
            if(backData.statusCode==200){
                consoleMes($("#manageContent"),2000,"success","正在导出excel文件！");
                window.open(gobalDownUrl+backData.msg);
            }else{
                consoleMes($("#manageContent"),2000,"warning",backData.msg);//success、info、warning、danger

            }
        }
    );
};
function getShowMes(type){
	if(type==null || type==""){
		 consoleMes($("#manageContent"),3000,"warning",'请选择盘点类型');
		 return;
	}
    var getUrl="";
    if(type==1){
        getUrl=gobalServer+"order/getChartByProductBrand"
    }else if(type==2){
        getUrl=gobalServer+"order/getChartByProductId"
    }else if(type==3){
        getUrl=gobalServer+"order/getChartByTimeYears"
    }else if(type==4){
        getUrl=gobalServer+"order/getChartByTimeMonths"
    }else if(type==5){
        getUrl=gobalServer+"order/getChartByTimeDays"
    }
    $.post(
        getUrl,
        {},
        function(backData){
            if(backData.statusCode==200){
                $("#statisticsList").show();
                indexStatisticsShow(backData);
            }else{
                consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
            }
        }
    );

};
function indexStatisticsShow(datarow){
	if(datarow!=null){
		$("#totalNum").text(datarow.totalCount);
		$("#totalMoney").text(datarow.totalPrice);
		/*显示表格*/
		statisticsEchart(datarow);
	}else{
		consoleMes($("#manageContent"),3000,"warning","无法获取统计数据，请刷新重试！");
	}
};
function statisticsEchart(datarow){
	if(datarow!=null){

        var xdata=[];
        var ydata=[];
        $.each(datarow.chartByCount, function(i,item) {
            xdata.push(item.xParam);
            ydata.push(item.yParam);
        });
        echartShow('echartShow1','库存出货量统计','出货量（件）',xdata,ydata);
        xdata=[];
        ydata=[];
        $.each(datarow.chartByPrice, function(i,item) {
            xdata.push(item.xParam);
            ydata.push(item.yParam);
        });
        echartShow('echartShow2','库存销售额统计','销售额（元）',xdata,ydata);
	}else{
		consoleMes($("#manageContent"),3000,"warning","无法相关数据，统计图表无法加载，请刷新重试！");
	}
};
/*表格显示*/
function echartShow(IDStr,title,legendstr,xdata,ydata){
	// 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById(IDStr));

	var colors = ['#5793f3', '#d14a61', '#675bba'];	
	
	option = {
	    title: {
	        text: title
	    },
	    tooltip: {
	        trigger: 'axis'
	    },
	    legend: {
	        data:[legendstr]
	    },
	    grid: {
	        left: '3%',
	        right: '4%',
	        bottom: '3%',
	        containLabel: true
	    },
	    toolbox: {
	        feature: {
	            saveAsImage: {}
	        }
	    },
	    xAxis: {
	        type: 'category',
	        boundaryGap: false,
	        data: xdata
	    },
	    yAxis: {
	        type: 'value'
	    },
	    series: [
	        {
	            name:legendstr,
	            type:'line',
	            stack: '总量',
	            data:ydata
	        }
	    ]
	};

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
};
